package br.com.it4.zmed.view.backbean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;

import org.apache.myfaces.custom.datascroller.HtmlDataScroller;
import org.apache.myfaces.custom.datascroller.ScrollerActionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.it4.framework.util.exception.SystemException;
import br.com.it4.framework.view.datamodel.PagedListDataModel;
import br.com.it4.zmed.entity.Atendimento;
import br.com.it4.zmed.entity.Convenio;
import br.com.it4.zmed.entity.Exame;
import br.com.it4.zmed.entity.Medico;
import br.com.it4.zmed.entity.Paciente;
import br.com.it4.zmed.filter.AtendimentoFilter;
import br.com.it4.zmed.filter.MedicoFilter;
import br.com.it4.zmed.filter.PacienteFilter;
import br.com.it4.zmed.service.AtendimentoService;
import br.com.it4.zmed.service.ConvenioService;
import br.com.it4.zmed.service.ExameService;
import br.com.it4.zmed.service.MedicoService;
import br.com.it4.zmed.service.PacienteService;

/**
 * Backing bean para o atendimento do paciente.
 */
@Component("atendimentoBean")
@Scope("session")
public class AtendimentoBean extends ZMedBean {

    @Autowired
    private PacienteService pacienteService;

    @Autowired
    private MedicoService medicoService;

    @Autowired
    private ConvenioService convenioService;

    @Autowired
    private ExameService exameService;

    @Autowired
    private AtendimentoService atendimentoService;

    private AtendimentoFilter atendimentoFilter;

    private List<Convenio> convenios;

    private List<Exame> exames;

    private PagedListDataModel<Atendimento> atendimentos;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() throws SystemException {
        this.atendimentoFilter = new AtendimentoFilter();
        this.atendimentoFilter.setPaciente(new Paciente());
        this.atendimentoFilter.setMedicoRequisitante(new Medico());
        this.convenios = this.convenioService.getConvenios();
        this.exames = this.exameService.getExames();
        
        this.updateDataModel(1);
    }

    /**
     * @return O atendimentoFilter a ser recuperado.
     */
    public AtendimentoFilter getAtendimentoFilter() {
        return atendimentoFilter;
    }

    /**
     * @param atendimentoFilter
     *            O atendimentoFilter a ser atribuido.
     */
    public void setAtendimentoFilter(AtendimentoFilter atendimentoFilter) {
        this.atendimentoFilter = atendimentoFilter;
    }

    /**
     * @return O atendimentos a ser recuperado.
     */
    public PagedListDataModel<Atendimento> getAtendimentos() {
        return atendimentos;
    }

    /**
     * @param atendimentos
     *            o atendimentos a ser atribuido.
     */
    public void setAtendimentos(PagedListDataModel<Atendimento> atendimentos) {
        this.atendimentos = atendimentos;
    }

    /**
     * @return O convenios a ser recuperado.
     */
    public List<Convenio> getConvenios() {
        return convenios;
    }

    /**
     * @return O exames a ser recuperado.
     */
    public List<Exame> getExames() {
        return exames;
    }

    public List<String> pacienteAutocompleteListener(String nomePaciente) throws SystemException {
        PacienteFilter filtro = new PacienteFilter();
        filtro.setNome(nomePaciente);
        List<Paciente> pacientes = this.pacienteService.getPacientes(filtro);

        List<String> pacienteAutocompleteList = new ArrayList<String>();

        for (Paciente paciente : pacientes) {
            pacienteAutocompleteList.add(paciente.getNome());
        }

        return pacienteAutocompleteList;
    }

    public List<String> medicoAutocompleteListener(String nomeMedico) throws SystemException {
        MedicoFilter filtro = new MedicoFilter();
        filtro.setNome(nomeMedico);
        List<Medico> medicos = this.medicoService.getMedicos(filtro);

        List<String> medicoAutocompleteList = new ArrayList<String>();

        for (Medico medico : medicos) {
            medicoAutocompleteList.add(medico.getNome());
        }

        return medicoAutocompleteList;
    }

    /**
     * Pesquisa os atendimentos realizados.
     * 
     * @throws SystemException
     *             Caso ocorra algum erro de sistema.
     */
    public void searchAtendimento() throws SystemException {
        this.updateDataModel(1);
    }

    public void pageListener(ActionEvent event) throws SystemException {
        ScrollerActionEvent scrollerEvent = (ScrollerActionEvent) event;
        int page = scrollerEvent.getPageIndex();

        if (page == -1) {
            if (scrollerEvent.getScrollerfacet().equals("next")) {
                page = ((HtmlDataScroller) scrollerEvent.getComponent()).getPageIndex() + 1;
            } else if (scrollerEvent.getScrollerfacet().equals("previous")) {
                page = ((HtmlDataScroller) scrollerEvent.getComponent()).getPageIndex() - 1;
            }
        }

        this.updateDataModel(page);
    }

    private void updateDataModel(int page) throws SystemException {

        int count = this.atendimentoService.getAtendimentosCount(this.atendimentoFilter).intValue();
        int pageSize = getPageSize();
        int firsRow = firstRow(page, pageSize);

        List<Atendimento> listaAtendimentos = this.atendimentoService.getAtendimentos(this.atendimentoFilter, firsRow,
                pageSize);

        if(this.atendimentos != null){
            this.atendimentos.setWrappedData(listaAtendimentos);
            this.atendimentos.setRowCount(count);
            this.atendimentos.setPageSize(pageSize);
        } else {
            this.atendimentos = new PagedListDataModel<Atendimento>(listaAtendimentos, count, pageSize);
        }
        
        this.atendimentos.setCurrentPage(page);
        
        if(page == 1){
            resetFirstPage("consultaAtendimentoForm:dataScroller");
        }
        
    }
}
